﻿// SilverlightDesktop - http://www.SilverlightDesktop.net
// Copyright (c) 2008
// by SilverlightDesktop
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;

using DotNetNuke;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using System.Collections.Generic;

namespace SilverlightDesktop.Module
{
    public partial class Settings : DotNetNuke.Entities.Modules.ModuleSettingsBase
    {
        string SDHorizontal;
        string SDVertical;
        bool SDTransparent;
        string SDAutoLoad;

        public override void LoadSettings()
        {
            try
            {
                if ((Page.IsPostBack == false))
                {

                    #region LoadValues
                    if ((ModuleSettings["SDHorizontal"]) == null)
                    {
                        SDHorizontal = "500";
                    }
                    else
                    {
                        SDHorizontal = Convert.ToString((ModuleSettings["SDHorizontal"]));
                    }

                    if ((ModuleSettings["SDVertical"]) == null)
                    {
                        SDVertical = "500";
                    }
                    else
                    {
                        SDVertical = Convert.ToString((ModuleSettings["SDVertical"]));
                    }

                    if ((ModuleSettings["SDTransparent"]) == null)
                    {
                        SDTransparent = false;
                    }
                    else
                    {
                        SDTransparent = Convert.ToBoolean((ModuleSettings["SDTransparent"]));
                    }

                    if ((ModuleSettings["SDAutoLoad"]) == null)
                    {
                        SDAutoLoad = "(none)";
                    }
                    else
                    {
                        SDAutoLoad = Convert.ToString((ModuleSettings["SDAutoLoad"]));
                    }
                    #endregion

                    LoadModuleDropDown();

                    DesktopSizeHorizontalTextBox.Text = SDHorizontal;
                    DesktopSizeVerticalTextBox.Text = SDVertical;
                    TransparentCheckBox.Checked = SDTransparent;
                    ddlAutoLoadModule.SelectedValue = SDAutoLoad;
                }
            }
            catch (Exception exc)
            {
                // Module failed to load
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        public override void UpdateSettings()
        {
            try
            {
                DotNetNuke.Entities.Modules.ModuleController objModules = new DotNetNuke.Entities.Modules.ModuleController();

                // Update Module Settings
                objModules.UpdateModuleSetting(ModuleId, "SDHorizontal", DesktopSizeHorizontalTextBox.Text.ToString());
                objModules.UpdateModuleSetting(ModuleId, "SDVertical", DesktopSizeVerticalTextBox.Text.ToString());
                objModules.UpdateModuleSetting(ModuleId, "SDTransparent", TransparentCheckBox.Checked.ToString());
                objModules.UpdateModuleSetting(ModuleId, "SDAutoLoad", ddlAutoLoadModule.SelectedValue);
            }
            catch (Exception exc)
            {
                // Module failed to load
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #region LoadModuleDropDown
        private void LoadModuleDropDown()
        {
            SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
            List<SilverlightDesktop.SilverlightDesktopModule> result = (from Module in SilverlightDesktopDAL.SilverlightDesktopModules
                                                                        select Module).ToList();

            SilverlightDesktop.SilverlightDesktopModule objSilverlightDesktopModule = new SilverlightDesktop.SilverlightDesktopModule();
            objSilverlightDesktopModule.ModuleID = 0;
            objSilverlightDesktopModule.ModuleName = "(none)";

            result.Add(objSilverlightDesktopModule);

            result = (from Module in result
                      orderby Module.ModuleID
                      select Module).ToList();

            ddlAutoLoadModule.SelectedValue = "0";
            ddlAutoLoadModule.DataSource = result;
            ddlAutoLoadModule.DataBind();
        }
        #endregion
    }
}